﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using PMC.Structs;
using System.Linq;

namespace PMC.Extensions.Default
{
    public partial class PtcExtensionControl : TabPage //Change to usercontrol to edit
    {
        private readonly BindingList<PTCWebsite> urls;
        private readonly PTCExtension context;

        public PtcExtensionControl(PTCExtension context)
        {
            InitializeComponent();
            Text = "PTC Clicker";
            this.context = context;
            
            urls = context.inputUrls;
            lvUrls.DataSource = urls;
            context.Finished += new EventHandler(context_Finished);
            lvUrls.EditMode = DataGridViewEditMode.EditProgrammatically;
            lvUrls.AllowUserToAddRows = false;
        }

        void context_Finished(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => context_Finished(sender, e)));
                return;
            }
            btnRun.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var dialog = new PTCForm();
            dialog.ShowDialog();

            if (dialog.DialogResult != DialogResult.OK)
                return;

            urls.Add(dialog.PtcWebsite);
            context.Changed = true;
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            btnRun.Enabled = false;
            context.Process();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (lvUrls.SelectedRows.Count > 0)
            {
                var dialog = new PTCForm((PTCWebsite) lvUrls.SelectedRows[0].DataBoundItem);
                dialog.ShowDialog();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in lvUrls.SelectedRows)
            {
                lvUrls.Rows.Remove(row);
            }
        }
    }
}
